Skip to content

Log request context in a middleware#536

Merged
GianlucaFicarelli merged 5 commits intomainfrom
log_request_context
Feb 24, 2026
Merged

Log request context in a middleware#536
GianlucaFicarelli merged 5 commits intomainfrom
log_request_context

Conversation

@GianlucaFicarelli
Copy link
Collaborator

@GianlucaFicarelli GianlucaFicarelli commented Feb 20, 2026

Since the previous PR added context to the application logs but not to the uvicorn logs, this PR adds a middleware for modifying the context and logging the requests, and changes the level of the uvicorn logger from INFO to WARNING to avoid logging duplicate information.

It's not possible to use the logger_context dependency as it was done in the previous PR, because it affects only the application logs emitted in the function using that dependency, so it won't add context to the logs in the middleware or uvicorn.

Now the logs are enriched with the following:

  • Extra logging keys for non-authenticated endpoints:
    • request_id
  • Extra logging keys for authenticated endpoints:
    • request_id
    • user_id

In addition to the above keys, thanks to the middleware the log of the request includes the following keys in the extra dict:

  • forwarded_for
  • process_time
  • status_code
  • client

In this way, when the logs are emitted as json, it should be easier to process them to extract the above information.

We might also consider adding other tools like opentelemetry in the future.

@GianlucaFicarelli GianlucaFicarelli self-assigned this Feb 20, 2026
@codecov
Copy link

codecov bot commented Feb 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
pytest 97.86% <100.00%> (+0.17%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
app/application.py 96.07% <100.00%> (-0.90%) ⬇️
app/config.py 97.43% <100.00%> (ø)
app/context.py 100.00% <100.00%> (ø)
app/dependencies/auth.py 100.00% <100.00%> (ø)
app/logger.py 76.00% <100.00%> (+38.22%) ⬆️
app/middleware.py 100.00% <100.00%> (ø)
app/routers/__init__.py 100.00% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@GianlucaFicarelli GianlucaFicarelli marked this pull request as ready for review February 23, 2026 14:10
@GianlucaFicarelli GianlucaFicarelli merged commit 46fbbb5 into main Feb 24, 2026
2 checks passed
@GianlucaFicarelli GianlucaFicarelli deleted the log_request_context branch February 24, 2026 08:22
GianlucaFicarelli added a commit that referenced this pull request Feb 24, 2026
* origin/main:
  Log request context in a middleware (#536)
  Add context information to each log message in authenticated endpoints (#535)
  Pin alembic<1.18 for compatibility with alembic-postgresql-enum (#533)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants